package com.haoan.index.service.impl;


import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import com.haoan.index.common.enums.ActivityStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.ActivityMapper;
import com.haoan.index.domain.Activity;
import com.haoan.index.service.ActivityService;

/**
 * 活动Service业务层处理
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper,Activity> implements ActivityService
{
    /**
     * 查询活动
     *
     * @param id 活动主键
     * @return 活动
     */
    @Override
    public Activity selectActivityById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询活动列表
     *
     * @param activity 活动
     * @return 活动
     */
    @Override
    public List<Activity> selectActivityList(Activity activity)
    {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(activity.getId() != null, Activity::getId, activity.getId());
        queryWrapper.eq(activity.getName() != null, Activity::getName, activity.getName());
        queryWrapper.eq(activity.getAddress() != null, Activity::getAddress, activity.getAddress());
        queryWrapper.eq(activity.getPicture() != null, Activity::getPicture, activity.getPicture());
        queryWrapper.eq(activity.getBrief() != null, Activity::getBrief, activity.getBrief());
        queryWrapper.eq(activity.getStartDate() != null, Activity::getStartDate, activity.getStartDate());
        queryWrapper.eq(activity.getEndDate() != null, Activity::getEndDate, activity.getEndDate());
        queryWrapper.eq(activity.getDetail() != null, Activity::getDetail, activity.getDetail());
        queryWrapper.eq(activity.getActivityStatus() != null, Activity::getActivityStatus, activity.getActivityStatus());
        queryWrapper.eq(activity.getCreateTime() != null, Activity::getCreateTime, activity.getCreateTime());
        queryWrapper.eq(activity.getCreateBy() != null, Activity::getCreateBy, activity.getCreateBy());
        queryWrapper.eq(activity.getUpdateTime() != null, Activity::getUpdateTime, activity.getUpdateTime());
        queryWrapper.eq(activity.getUpdateBy() != null, Activity::getUpdateBy, activity.getUpdateBy());
        queryWrapper.eq(Activity::getValiFlag,ValiFlag.VALID );
        queryWrapper.orderByDesc(Activity::getCreateTime);
        List<Activity> list = baseMapper.selectList(queryWrapper);
        list.forEach(e ->{
            if (e.getStartDate().before(new Date()) && e.getEndDate().after(new Date()) ){
                e.setStatus("进行中");
            }else if (e.getStartDate().after(new Date())){
                e.setStatus("待开始");
            }else {
                e.setStatus("已结束");
            }
        });
        return list;
    }

    /**
     * 新增活动
     *
     * @param activity 活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertActivity(Activity activity)
    {
        return baseMapper.insert(activity);
    }

    /**
     * 修改活动
     *
     * @param activity 活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateActivity(Activity activity)
    {
        return baseMapper.updateById(activity);
    }

    /**
     * 批量删除活动
     *
     * @param ids 需要删除的活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivityByIds(Long [] ids)
    {
        Activity activity = new Activity();
        activity.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<Activity> updQw = new LambdaQueryWrapper<>();
        updQw.in(Activity::getId, ids);
        return baseMapper.update(activity, updQw);
    }

    /**
     * 删除活动信息
     *
     * @param id 活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        Activity activity = getById(id);
        activity.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(activity);
    }

    @Override
    public int manage(Long id) {
        Activity activity = getById(id);
        // 判断状态
        ActivityStatusEnum activityStatus = ActivityStatusEnum.getByCode(activity.getActivityStatus());
        if (ActivityStatusEnum.NEW == activityStatus || ActivityStatusEnum.OFF == activityStatus) {
            activity.setActivityStatus(ActivityStatusEnum.ON.getCode());
        } else {
            activity.setActivityStatus(ActivityStatusEnum.OFF.getCode());
        }
        return baseMapper.updateById(activity);
    }
}
